package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import com.itheima.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Author chongge
 * Time 2019/10/30 11:20
 * ClassName com.itheima.service.impl
 */
@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberDao memberDao;

    /***
     * 根据电话号码查询用户是否存在
     * @param telephone
     * @return
     */
    @Override
    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }

    /***
     * 增加用户
     * @param member
     */
    @Override
    public void add(Member member) {
        if(member.getPassword() != null){
            member.setPassword(MD5Utils.md5(member.getPassword()));
        }
        memberDao.add(member);
    }

    /***
     * 根据月份查询会员数量
     * @param months
     * @return
     */
    @Override
    public List<Integer> findMemberCountByMonth(List<String> months) {
        List<Integer> list = new ArrayList<>();
        for (String month : months) {
            month = month+"-31";
            Integer count = memberDao.findMemberCountByMonth(month);
            list.add(count);
        }
        return list;
    }

    @Override
    public List<Map<String, Object>> getMemberGenderReport() {

        return memberDao.getMemberGenderReport();
    }

    /**
     * 根据年龄段查找会员人数，以及所占比例
     * @return
     */

    @Override
    public List<Map<String, Object>> getMemberAgeRangeReport() {
        List<Map<String, Object>> list = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        //查询0-18的人数
        String endDate = simpleDateFormat.format(new Date());
        calendar.add(Calendar.YEAR,-18);
        Date date = calendar.getTime();
        String beginDate = simpleDateFormat.format(date);
        Map<String,String> parasMap = new HashMap<>();
        parasMap.put("beginDate",beginDate);
        parasMap.put("endDate",endDate);
        Integer count1 = memberDao.findMemberCountByDateRange(parasMap);
        Map<String,Object> map1 = new HashMap<>();
        map1.put("name","0-18岁");
        map1.put("value",count1);
        list.add(map1);
        //查询18-30的人数
        Map<String,Object> map2 = new HashMap<>();
        endDate = beginDate;
        calendar.add(Calendar.YEAR,-30);
        beginDate = simpleDateFormat.format(calendar.getTime());
        parasMap.put("beginDate",beginDate);
        parasMap.put("endDate",endDate);
        Integer count2 = memberDao.findMemberCountByDateRange(parasMap);
        map2.put("name","18-30岁");
        map2.put("value",count2);
        list.add(map2);
        //30-45的人数
        Map<String,Object> map3 = new HashMap<>();
        endDate = beginDate;
        calendar.add(Calendar.YEAR,-45);
        beginDate = simpleDateFormat.format(calendar.getTime());
        parasMap.put("beginDate",beginDate);
        parasMap.put("endDate",endDate);
        Integer count3 = memberDao.findMemberCountByDateRange(parasMap);
        map3.put("name","30-45岁");
        map3.put("value",count3);
        list.add(map3);
        //45岁以上的人数
        Map<String,Object> map4 = new HashMap<>();
        Integer count4 = memberDao.findMemberCountAfterBirthday(beginDate);
        map4.put("name","45岁以上");
        map4.put("value",count4);
        list.add(map4);
        return list;
    }
}
